if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_HangHoa_GetAll]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[sp_HangHoa_GetAll]
GO

CREATE PROCEDURE [sp_HangHoa_GetAll]
@Ma_hang 		varchar(50),
@Ten_hang 		nvarchar(50)
AS
Begin
	if @Ma_hang ='' and @Ten_hang  = ''
	Begin	
	SELECT	 
			A.Ma_hang, 
			A.Ten, 
			A.ID,
			A.Ma_nhom_hang, 
			B.Ten AS Ten_nhom_hang, 
			A.Thue, 
			A.Chiet_khau, 
			A.Ghi_chu, 
			A.Ma_don_vi_qui_doi,
			C.Don_vi_goc, 
			A.Gia_nhap, 
			A.Gia_ban_si, 
			A.Gia_ban_le, 
			A.Ton_dinh_muc, 
			A.Mo_ta,
			A.Barcode_new,
			A.Ngay_cap_nhat_cuoi
		FROM HangHoa A
		INNER JOIN NhomHang B ON A.Ma_nhom_hang = B.ID
		INNER JOIN DonViQuiDoi C ON A.Ma_don_vi_qui_doi = C.ID
	End

	if @Ma_hang <>'' and @Ten_hang  = ''
	Begin
	SELECT	 
			A.Ma_hang, 
			A.Ten, 
			A.ID,
			A.Ma_nhom_hang, 
			B.Ten AS Ten_nhom_hang, 
			A.Thue, 
			A.Chiet_khau, 
			A.Ghi_chu, 
			A.Ma_don_vi_qui_doi,
			C.Don_vi_goc, 
			A.Gia_nhap, 
			A.Gia_ban_si, 
			A.Gia_ban_le, 
			A.Ton_dinh_muc, 
			A.Mo_ta,
			A.Barcode_new,
			A.Ngay_cap_nhat_cuoi
		FROM HangHoa A
		INNER JOIN NhomHang B ON A.Ma_nhom_hang = B.ID
		INNER JOIN DonViQuiDoi C ON A.Ma_don_vi_qui_doi = C.ID
		where A.Ma_hang LIKE '%' + @Ma_hang + '%'
	End

	
	if @Ma_hang <>'' and @Ten_hang  <> ''
	Begin	
	SELECT	 
			A.Ma_hang, 
			A.Ten, 
			A.ID,
			A.Ma_nhom_hang, 
			B.Ten AS Ten_nhom_hang, 
			A.Thue, 
			A.Chiet_khau, 
			A.Ghi_chu, 
			A.Ma_don_vi_qui_doi,
			C.Don_vi_goc, 
			A.Gia_nhap, 
			A.Gia_ban_si, 
			A.Gia_ban_le, 
			A.Ton_dinh_muc, 
			A.Mo_ta,
			A.Barcode_new,
			A.Ngay_cap_nhat_cuoi
		FROM HangHoa A
		INNER JOIN NhomHang B ON A.Ma_nhom_hang = B.ID
		INNER JOIN DonViQuiDoi C ON A.Ma_don_vi_qui_doi = C.ID
		where A.Ma_hang LIKE '%' + @Ma_hang + '%'
		and   A.Ten LIKE '%' + @Ten_hang  + '%'
		
	End
	
	if @Ma_hang ='' and @Ten_hang  <> ''
	Begin	
	
	SELECT	 
			A.Ma_hang, 
			A.Ten, 
			A.ID,
			A.Ma_nhom_hang, 
			B.Ten AS Ten_nhom_hang, 
			A.Thue, 
			A.Chiet_khau, 
			A.Ghi_chu, 
			A.Ma_don_vi_qui_doi,
			C.Don_vi_goc, 
			A.Gia_nhap, 
			A.Gia_ban_si, 
			A.Gia_ban_le, 
			A.Ton_dinh_muc, 
			A.Mo_ta,
			A.Barcode_new,
			A.Ngay_cap_nhat_cuoi
		FROM HangHoa A
		INNER JOIN NhomHang B ON A.Ma_nhom_hang = B.ID
		INNER JOIN DonViQuiDoi C ON A.Ma_don_vi_qui_doi = C.ID
		where  A.Ten LIKE '%' + @Ten_hang  + '%'
		
	End
	

End